const dict_config_keys = require('../dict_config_keys')
const dict_src_type = require('../dict_src_type')
const utils = require('../utils')
const fs = require('fs')

class DictHeader {
    constructor(headerPathOrStr, markdownKey, isPath = false) {
        this.headerPathOrStr_ = headerPathOrStr;
        this.isPath_ = isPath
        this.markdownKey_ = markdownKey;
        this.contents_ = '';
        this.config_ = null;
        this.parse();
    }

    isValid() {
        return this.config_ &&
            this.getItem(dict_config_keys.name) &&
            this.getItem(dict_config_keys.version) &&
            this.getItem(dict_config_keys.id) &&
            this.getItem(dict_config_keys.type)
    }

    getJson() {
        return this.config_
    }

    getJsonString() {
        return JSON.stringify(this.config_)
    }

    getType() {
        return this.getItem(dict_config_keys.type)
    }

    isTextType() {
        return this.getType() === dict_src_type.type.text;
    }

    isImageType() {
        return this.getType() === dict_src_type.type.image;
    }

    isTextParts() {
        return this.isTextType() && !this.contents_;
    }

    isImageWordWordType() {
        return this.isImageType() && this.getItem(dict_config_keys.image_type) === dict_src_type.image_type.image_word_word;
    }

    isImageImageWordType() {
        return this.isImageType() && this.getItem(dict_config_keys.image_type) === dict_src_type.image_type.image_image_word;
    }

    getItem(key) {
        return this.config_ && this.config_[key]
    }

    parse() {
        const str = this.isPath_
            ? fs.readFileSync(this.headerPathOrStr_, { encoding: 'utf-8' })
            : this.headerPathOrStr_
        if (!str) return null
        const markdownKey = this.markdownKey_;

        const contentPos = str.indexOf('\n@contents:');
        let header_str = '';
        if (contentPos < 0) {
            header_str = str.trim();
        } else {
            header_str = str.substr(0, contentPos).trim();
            this.contents_ = str.substr(contentPos + 1 + '@contents:'.length).trim()
        }

        const ret = {}
        const lines = header_str.split('\n')
        lines.every((item, i) => {
            if (markdownKey) {
                const reg = new RegExp(`^@${markdownKey}:([\s\S]*)`, 'g')
                if (reg.test(item)) {
                    let mdContent = RegExp.$1
                    mdContent += lines.slice(i + 1).join('\n')
                    ret[markdownKey] = mdContent.trim();
                    return false
                }
            }


            if (/^@([^:]+):([\s\S]+)/g.test(item)) {
                ret[RegExp.$1] = String(RegExp.$2).trim();
            }

            return true;
        })

        if (ret.js) {
            ret.js = ret.js.split(',')
        }

        if (ret.css) {
            ret.css = ret.css.split(',')
        }

        this.config_ = ret;
        // create_time
        // update_time
        const nowStr = utils.formatDate(new Date(), 'yyyy-MM-dd HH:mm:ss');
        if (!this.config_[dict_config_keys.create_time]) {
            this.config_[dict_config_keys.create_time] = nowStr;
        }
        this.config_[dict_config_keys.update_time] = nowStr;
    }

    getContents() {
        return this.contents_;
    }

}

module.exports = DictHeader;